package cn.wanho.service.impl;

import cn.wanho.dao.*;
import cn.wanho.dto.*;
import cn.wanho.rvo.*;
import cn.wanho.service.CmsClassStageService;
import cn.wanho.service.CmsTeacherService;
import cn.wanho.util.QiniuUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service
public class CmsTeacherServiceImpl implements CmsTeacherService {

    private final String EXAM = "wanhocms:exam:answerpaper:";
    @Resource
    private CmsClassStageService cmsClassStageService;
    @Resource
    private CmsScoreService cmsScoreService;

    @Resource
    private CmsClassMapper cmsClassMapper;


    @Resource
    private CmsExamMapper cmsExamMapper;

    @Resource
    private CmsUserMapper cmsUserMapper;

    /**
     * 考试汇总表
     */
    @Resource
    private CmsExamCountMapper cmsExamCountMapper;


    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private CmsClassStageMapper cmsClassStageMapper;

    /**
     *查看该老师当前所带的班级
     * @param teacherId
     * @return
     */
    @Override
    public ResultVO selectTeacherInClass(Integer teacherId) {

        List<CmsClass> cmsClasses = cmsClassMapper.selectTeacherInClass(teacherId);

        if (cmsClasses.isEmpty()){

            return ResultVO.failure(ResultEnum.SELECT_TEACHER_IN_CLASS_FAILURE.getResCode(), ResultEnum.SELECT_TEACHER_IN_CLASS_FAILURE.getResMsg());

        }else {
            return ResultVO.success(ResultEnum.SELECT_TEACHER_IN_CLASS_SUCESS.getResCode(), ResultEnum.SELECT_TEACHER_IN_CLASS_SUCESS.getResMsg(),cmsClasses);
        }

    }

    /**
     * 根据班级id查询学生
     * @param classId
     * @return
     */
    @Override
    public ResultVO selectStudentsByClassId(Integer classId) {
        List<SelectStudentsByClassIdVO> students= cmsUserMapper.selectStudentsByClassId(classId);
        if (students.isEmpty()){

            return ResultVO.failure(ResultEnum.SELECT_STUDENT_BYCLASSID_FAILURE.getResCode(), ResultEnum.SELECT_STUDENT_BYCLASSID_FAILURE.getResMsg());

        }else {
            return ResultVO.success(ResultEnum.SELECT_STUDENT_BYCLASSID_SUCESS.getResCode(), ResultEnum.SELECT_STUDENT_BYCLASSID_SUCESS.getResMsg(),students);
        }

    }

    /**
     * 根据id查找学生信息
     *
     * @param studentId
     * @return
     */
    @Override
    public ResultVO selectStudentBystuId(Integer studentId) {
        List<selectStudentBystuIdVO> student =  cmsUserMapper.selectStudentBystuId(studentId);


        if (student.isEmpty()){

            return ResultVO.failure(ResultEnum.SELECT_STUDENT_BYSTUDENTID_FAILURE.getResCode(), ResultEnum.SELECT_STUDENT_BYSTUDENTID_FAILURE.getResMsg());

        }else {
            return ResultVO.success(ResultEnum.SELECT_STUDENT_BYSTUDENTID_SUCCESS.getResCode(), ResultEnum.SELECT_STUDENT_BYSTUDENTID_SUCCESS.getResMsg(),student);
        }
    }

    /**
     * 默认查看的是老师当前所带班级的学生信息
     * @param teacherId   SELECT_DEFAULT_STUDENTS_FAILURE
     * @return
     */
    @Override
    public ResultVO selectDefaultStudents(Integer teacherId,Integer page,Integer rows) {
        PageHelper.startPage(page,rows);
        List<SelectTeacherInClassStudentsVO> students =cmsUserMapper.selectDefaultStudents(teacherId);

        PageInfo<SelectTeacherInClassStudentsVO> bbsArticlePageInfo = new PageInfo<>(students);
        if (students.isEmpty()){

            return ResultVO.failure(ResultEnum.SELECT_DEFAULT_STUDENTS_FAILURE.getResCode(), ResultEnum.SELECT_DEFAULT_STUDENTS_FAILURE.getResMsg());

        }else {
            return ResultVO.success(ResultEnum.SELECT_DEFAULT_STUDENTS_SUCCESS.getResCode(), ResultEnum.SELECT_DEFAULT_STUDENTS_SUCCESS.getResMsg(),bbsArticlePageInfo);
        }
    }

    /**
     * 修改老师个人信息（含头像）
     *
     * @param bytes
     * @param introduction
     * @return
     */
    @Override
    public ResultVO modifySelf(byte[] bytes, String introduction,Integer teacherId) {

        String path = QiniuUtil.upload(bytes);
        int i = 0 ;
        i = cmsUserMapper.modifySelf(path,introduction,teacherId);

        if (i > 0 ){
            return ResultVO.success(ResultEnum.UPDATE_TEACHER_INFORMATION_SUCCESS.getResCode(),ResultEnum.UPDATE_TEACHER_INFORMATION_SUCCESS.getResMsg());
        }else {
            return  ResultVO.failure(ResultEnum.UPDATE_TEACHER_INFORMATION_FAILURE.getResCode(), ResultEnum.UPDATE_TEACHER_INFORMATION_FAILURE.getResMsg());
        }

    }

    /**
     * 创建试卷并设置考试时间
     * @return
     */
    @Override
    public ResultVO setPaperAndStage(CmsClassStage cmsClassStage) {

        int i = 0;
        i =cmsClassStageMapper.setPaperAndStage(cmsClassStage);
        if (i != 0){
            return ResultVO.success(ResultEnum.SET_CLASS_STAGE_SUCCESS.getResCode(),ResultEnum.SET_CLASS_STAGE_SUCCESS.getResMsg());
        }else {
            return ResultVO.failure(ResultEnum.SET_CLASS_STAGE_FAILURE.getResCode(),ResultEnum.SET_CLASS_STAGE_FAILURE.getResMsg());
        }

    }

    /**
     * 单题打分
     *
     * @param questionId
     * @param score
     * @return
     */
    @Override
    public ResultVO setScore(Integer questionId, Integer score) {
        int i = 0;
        i = cmsExamCountMapper.setScore(questionId,score);
        if (i != 0){
            return ResultVO.success(ResultEnum.ADD_SCORE_SUCCESS.getResCode(),ResultEnum.ADD_SCORE_SUCCESS.getResMsg());
        }else {
            return ResultVO.success(ResultEnum.ADD_SCORE_FAILURE.getResCode(),ResultEnum.ADD_SCORE_FAILURE.getResMsg());
        }

    }

    /**
     * 目前授课的班级以及对阶段和目前当前所处阶段完结剩余的时间
     *
     * @param teacherId
     * @return
     */
    @Override
    public ResultVO selectPresentClass(Integer teacherId) {

        List<selectPresentClassVO> list = cmsClassMapper.selectPresentClass(teacherId);
        if (list.isEmpty()){

            return ResultVO.failure(ResultEnum.NOW_CLASS_STAGE_FAILURE.getResCode(), ResultEnum.NOW_CLASS_STAGE_FAILURE.getResMsg());

        }else {
            return ResultVO.success(ResultEnum.NOW_CLASS_STAGE_SUCCESS.getResCode(), ResultEnum.NOW_CLASS_STAGE_SUCCESS.getResMsg(),list);
        }


    }

    /**
     * 计算总分
     *
     * @param studentId
     * @param paperId
     * @return
     */
    @Override
    public ResultVO getSumScore(Integer studentId, Integer paperId) {
        System.out.println(studentId+"?"+paperId);
        String k1 ="wanhocms:exam:answerpaper:"+paperId+":"+studentId+":object";
        String k2 ="wanhocms:exam:answerpaper:"+paperId+":"+studentId+":subject";
        System.out.println(k1);
        Set<CmsExamCount> set1 = redisTemplate.opsForZSet().range(k1, 0, -1);
        System.out.println(set1.isEmpty());
        Set<CmsExamCount> set2 = redisTemplate.opsForZSet().range(k2, 0, -1);
        Integer sum = 0;
        for (CmsExamCount cmsExamCount : set1) {
            System.out.println(cmsExamCount);
            sum =sum+cmsExamCount.getScore();
            int i = cmsExamCountMapper.insert(cmsExamCount);
            System.out.println("===============>"+":"+i);
        }
        for (CmsExamCount cmsExamCount : set2) {
            cmsExamCountMapper.insert(cmsExamCount);
            sum =sum+cmsExamCount.getScore();
        }
       /* List<CmsExamCount> sumScore =cmsExamCountMapper.getSumScore(studentId,paperId);
        for (CmsExamCount cmsExamCount : sumScore) {

            System.out.println();
        }*/


        GetSumScoreVO sumScoreVO = new GetSumScoreVO(sum);
        CmsScore cmsScore = new CmsScore();
        if (sum<60){
            cmsScore.setEvaluate("不及格");
        }else if(sum>=60&&sum<80){
            cmsScore.setEvaluate("及格");
        }else if(sum>=80&&sum<90){
            cmsScore.setEvaluate("良好");
        }else if(sum>=90){
            cmsScore.setEvaluate("优秀");
        }
        cmsScore.setScore(sum);
        cmsScore.setStudentId(studentId);
        Integer classStageId = cmsClassStageService.getClassStage(paperId);
        cmsScore.setClassStageId(classStageId);
        cmsScoreService.insertScore(cmsScore);
            return ResultVO.success(ResultEnum.GET_SUM_SCORE_SUCCESS.getResCode(), ResultEnum.GET_SUM_SCORE_SUCCESS.getResMsg(),sumScoreVO);
    }

    /**
     * 简答题打分（redis）
     *
     * @param studentId
     * @param paperId
     * @return
     */
    @Override
    public ResultVO setScore2(Integer studentId, Integer paperId,Integer sortId,Integer score) {
        String key3 = EXAM+paperId+":"+studentId+":subject";
        System.out.println(key3);
        System.out.println(sortId);
        Set<CmsExamCount> set= redisTemplate.opsForZSet().reverseRangeByScore(key3, sortId, sortId);
        //set-->list
        for (CmsExamCount cmsExamCount : set) {
            System.out.println(cmsExamCount);
        }
        List <CmsExamCount> list = new ArrayList<>(set);
        //遍历集合找到对应的简答题
        CmsExamCount cmsExamCount=list.get(0);
        // 移除
        redisTemplate.opsForZSet().removeRangeByScore(key3,sortId,sortId);
        cmsExamCount.setScore(score);
        // 打分添加
        redisTemplate.opsForZSet().add(key3,cmsExamCount,sortId);
        return ResultVO.success(ResultEnum.SET_SCORE_END.getResCode(), ResultEnum.SET_SCORE_END.getResMsg());
    }

}
